home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / Gfx / Edit / TSMorph / src / Args.c < prev    next >
C/C++ Source or Header  |  1994-10-30  |  9KB  |  297 lines

  1. // TSMorph - Amiga Morphing program
  2. // Copyright (C) © 1993  Topicsave Limited
  3.  
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; either version 2 of the License, or
  7. // any later version.
  8.  
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. // GNU General Public License for more details.
  13.  
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program; if not, write to the Free Software
  16. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. // mpaddock@cix.compulink.co.uk
  19.  
  20. /* This object is included in both TSMorph and TSMorph-render
  21.  * it includes all the stuff for determing arguments
  22.  * including settings file stuff
  23.  */
  24.  
  25. // include headers since does not include precompiled headers
  26. #include <workbench/workbench.h>
  27. #include <workbench/startup.h>
  28. #include <proto/icon.h>
  29. #include <proto/dos.h>
  30. #include <proto/exec.h>
  31. #include <exec/memory.h>
  32. #include <stddef.h>
  33. #include <string.h>
  34.  
  35. // The program icon for settings
  36. struct DiskObject *program_icon = NULL;
  37.  
  38. /* My version of ArgArrayDone
  39.  * does not use cx_lib version as would have to open commodities.library
  40.  * frees everything allocated by argArrayInit
  41.  */
  42. void
  43. argArrayDone( void ) {
  44.     if (program_icon) {
  45.         FreeDiskObject(program_icon);
  46.     }
  47. }
  48.  
  49. /* my version for ArgArrayInit
  50.  * returns   : pointer to the parameters or program tool types
  51.  * parameters: argc
  52.  *             argv - standard main() arguments
  53.  */
  54. char **
  55. argArrayInit( LONG argc, char **argv ) {
  56.     if (argc) {                // from the shell
  57.         argv[argc] = 0;    // HACK!!!!!
  58.         return argv;        // The above is required to prevent the following code
  59.     }                            // falling off the end. Not sure how safe this is!!!
  60.     else {
  61.         struct WBStartup *wbs = (struct WBStartup *) argv;                                // from the Workbench
  62.         if (program_icon = GetDiskObject((char *) wbs->sm_ArgList->wa_Name)) {    // get the program icon
  63.             return ((char **) program_icon->do_ToolTypes);                                // return tooltypes
  64.         }
  65.     }
  66.     return NULL;
  67. }
  68.  
  69. /* My version of ArgString
  70.  * returns   : string result
  71.  * parameters: arg1 - parameter array
  72.  *             arg2 - parameter name
  73.  *             arg3 - default value
  74.  */
  75. UBYTE *
  76. ArgString( UBYTE **arg1, UBYTE *arg2, UBYTE *arg3 ) {
  77.     UBYTE *s;
  78.     if (arg1) {
  79.         if (s = FindToolType(arg1,arg2)) {
  80.             return s;
  81.         }
  82.     }
  83.     return arg3;
  84. }
  85.  
  86. /* The following defines are what make this code specific to TSMorph
  87.  * they could be changed for another program
  88.  */
  89. #define PREFSDIR "ENV:TSMorph/"        // Directory where prefs may be held
  90. #define PREFSFILE "TSMorph.prefs"    // Name of prefs file
  91.  
  92. struct DiskObject    *project_icon             = NULL;    // The project Icon
  93. char                    **project_settings    = NULL;    // Parameters on project Icon
  94. extern char            **ArgArray;                            // Program Parameter/ToolTypes
  95. extern char            **ArgArraySettings;                // Parameters from settings file
  96.  
  97. BOOL                    FromWB;                                // Are we shell or Workbench
  98.  
  99. /* Similar to ArgArrayInit but knows
  100.  * how to handle settings files
  101.  * does not return a value - it is held in this file
  102.  */
  103. void
  104. MyArgArrayInit(int argc,char **argv) {
  105.     BPTR     oldcurrentdir     = NULL;    // Remember the current dir
  106.     BPTR     settings            = 0;        // Settings file handle
  107.     UBYTE    string[65];                    //    buffer to read in (should not be limited size)
  108.     int     kount                = 0,        // number of settings strings in file
  109.             kount1            = 0;        // index
  110.     UBYTE *settingsname;                // file name of settings
  111.     BPTR    projectdir        = 0;        // project directory
  112.     struct WBStartup    *argmsg;        // Workbench message
  113.     struct WBArg         *wb_arg;        // Workbench argument
  114.     /* check if from workbench    */
  115.     if (!argc) {
  116.         FromWB = TRUE;
  117.           argmsg = (struct WBStartup *)argv;
  118.         if (argmsg->sm_NumArgs > 1) {
  119.             wb_arg = argmsg->sm_ArgList;
  120.              wb_arg++;
  121.              projectdir = wb_arg->wa_Lock;                    // set up project dir for settings file
  122.             oldcurrentdir = CurrentDir(projectdir);    // switch to project dir
  123.             if (project_icon = GetDiskObject((char *) wb_arg->wa_Name)) {
  124.                 project_settings = (char **) project_icon->do_ToolTypes;    // and get project tool types if poss.
  125.             }
  126.             CurrentDir(oldcurrentdir);                        // switch back to original dir
  127.             oldcurrentdir = NULL;
  128.         }
  129.     }
  130.     else {
  131.         FromWB = FALSE;
  132.     }
  133.     // Do normal ArgArrayInit
  134.     ArgArray = argArrayInit(argc,argv);
  135.     // Get name of settings file
  136.     ArgArraySettings = NULL;
  137.     settingsname = ArgString(project_settings,"SETTINGS",NULL);    // first from SETTINGS= on project
  138.     if (settingsname) {
  139.         settings = Open(settingsname,MODE_OLDFILE);                    // try and open if present
  140.     }
  141.     if (!settings) {
  142.         settingsname = ArgString(ArgArray,"SETTINGS",NULL);        // if not open try Program parameters/tool types SETTINGS=
  143.     }
  144.     // try and open
  145.     if (settingsname && !settings) {
  146.         settings = Open(settingsname,MODE_OLDFILE);                    // and try and open if present
  147.     }
  148.     if (!settings) {
  149.         /* if not open then if from WorkBench then change
  150.          * to the project dir, if from CLI try current dir
  151.          */
  152.         if (projectdir) {
  153.             oldcurrentdir = CurrentDir(projectdir);
  154.         }
  155.         if (!(settings = Open(settingsname = PREFSFILE,MODE_OLDFILE))) {    // try and open again in "current" dir
  156.             // change back if it did not open
  157.             if (projectdir) {
  158.                 CurrentDir(oldcurrentdir);
  159.             }
  160.         }
  161.     }
  162.     // if still not open then try program directory
  163.     if (!settings) {
  164.         settings = Open(settingsname = "PROGDIR:"PREFSFILE,MODE_OLDFILE);    //    try and open again in program dir
  165.     }
  166.     // still not open so try ENV:
  167.     if (!settings) {
  168.         settings = Open(settingsname = PREFSDIR PREFSFILE,MODE_OLDFILE);    // and again!!! in ENV:/xxx/
  169.     }
  170.     if (settings) {                                                                        // We have a settings file!!!
  171.         // If we have found a file then count the relevant lines
  172.         while (FGets(settings,string,64)) {        // read in each line
  173.             if ((string[0] != '\n') &&                // ignore blank lines
  174.                  (string[0] != ';')) {                // and commented out lines
  175.                 ++kount;                                    // count the rest
  176.             }
  177.         }
  178.         Close(settings);                                // close the file
  179.         if (settings = Open(settingsname,MODE_OLDFILE)) {    // reopen it (why not seek?)
  180.             // Allocate memory for settings from file pointers (including zero at end
  181.             if (ArgArraySettings = AllocVec((kount+1) * sizeof(UBYTE *),MEMF_CLEAR)) {
  182.                 // Read in all settings
  183.                 while (FGets(settings,string,64) &&
  184.                          (kount1 < kount)) {                                    // Do not do too many
  185.                     // remove newline (from FGets)
  186.                     string[strlen(string)-1] = '\0';
  187.                     // ignore blank and comment lines
  188.                     if (string[0] && (string[0]!=';')) {
  189.                         // Clone settings in memory
  190.                         ArgArraySettings[kount1] = strdup(string);    // Note!
  191.                         ++kount1;                                                // strdup() not AllocMem() etc.
  192.                     }                                                                // so memory is only freed on quit
  193.                 }
  194.             }
  195.             // Close file and change directory back if required
  196.             Close(settings);
  197.         }
  198.         if (oldcurrentdir) {
  199.             CurrentDir(oldcurrentdir);
  200.         }
  201.     }
  202. }
  203.  
  204. /* Similar to ArgArrayDone but knows
  205.  * how to handle settings files
  206.  */
  207. void
  208. MyArgArrayDone(void) {
  209.     // Call argArrayDone
  210.     argArrayDone();
  211.     // and free settings file pointers
  212.     if (ArgArraySettings) {
  213.         FreeVec(ArgArraySettings);
  214.         ArgArraySettings = NULL;
  215.     }
  216.     // and the project icon
  217.     if (project_icon) {
  218.         FreeDiskObject(project_icon);
  219.         project_icon = NULL;
  220.     }
  221. }
  222.  
  223. /* Similar to ArgString but knows
  224.  * how to handle settings files
  225.  * missing parameter: arg1   - is internal
  226.  * extra parameter  : reopen - set if this is not the initial call but just a new project
  227.  */
  228. UBYTE *
  229. MyArgString(UBYTE *arg2,UBYTE *arg3,BOOL reopen) {
  230.     UBYTE *s;
  231.     // Look in project Parameters first
  232.     if (project_settings) {
  233.         if (s = FindToolType(project_settings,arg2)) {
  234.             return s;
  235.         }
  236.     }
  237.     if (!reopen) {
  238.         // then look in parameters
  239.         if (ArgArray) {
  240.             if (s = FindToolType(ArgArray,arg2)) {
  241.                 return s;
  242.             }
  243.         }
  244.         // then in settings file
  245.         if (ArgArraySettings) {
  246.             if (s = FindToolType(ArgArraySettings,arg2)) {
  247.                 return s;
  248.             }
  249.         }
  250.     }
  251.     return arg3;
  252. }
  253.  
  254. /* Similar to ArgInt but knows
  255.  * how to handle settings files
  256.  * See MyArgString() for different parameters
  257.  */
  258. LONG
  259. MyArgInt(UBYTE *arg2, long arg3,BOOL reopen) {
  260.     UBYTE *s;
  261.     LONG ret;
  262.     if (s = MyArgString(arg2,NULL,reopen)) {
  263.         StrToLong(s,&ret);
  264.         return ret;
  265.     }
  266.     return arg3;
  267. }
  268.  
  269. /* Similar to FindToolType but knows
  270.  * how to handle settings files
  271.  * See MyArgString() for different parameters
  272.  */
  273. BOOL MyFindToolType(UBYTE *arg2,BOOL reopen) {
  274.     if (MyArgString(arg2,NULL,reopen)) {
  275.         return TRUE;
  276.     }
  277.     return FALSE;
  278. }    
  279.  
  280. /* Opens a new project parameters
  281.  * Frees old Icon and opens new, if from Workbench
  282.  * filename must be full file name
  283.  */
  284. void OpenNewArgs(UBYTE *filename) {
  285.     if (FromWB) {
  286.         if (project_icon) {
  287.             FreeDiskObject(project_icon);
  288.         }
  289.         if (project_icon = GetDiskObject(filename)) {
  290.             project_settings = (char **) project_icon->do_ToolTypes;
  291.         }
  292.         else {
  293.             project_settings = NULL;
  294.         }
  295.     }
  296. }
  297.